package com.evan.wj.controller;

import com.evan.wj.pojo.Bookshelf;
import com.evan.wj.pojo.User;
import com.evan.wj.pojo.ViewBS;
import com.evan.wj.result.Result;
import com.evan.wj.result.ResultFactory;
import com.evan.wj.service.BookshelfService;
import com.evan.wj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
public class BookshelfContraller {
    @Autowired
    BookshelfService bookshelfService;
    @Autowired
    UserService userService;

    @RequestMapping("/api/bookshelf/personal")
    public Result personalBookshelf(@RequestBody User user){

        User byName = userService.getByName(user.getUsername());
        List<ViewBS> viewBS = bookshelfService.personalBookshelf(byName.getId());
        return ResultFactory.buildSuccessResult(viewBS);
    }

    @PostMapping("/api/bookshelf/delete")
    public Result delete(@RequestBody Bookshelf bookshelf){

        bookshelfService.delete(bookshelf);
        return ResultFactory.buildSuccessResult(null);
    }

    @PostMapping("/api/bookshelf/add")
    public Result add(@RequestBody ViewBS viewBS){
        User byName = userService.getByName(viewBS.getUsername());
        Bookshelf bookshelf = new Bookshelf();
        bookshelf.setBookId(viewBS.getBookId());
        bookshelf.setUserId(byName.getId());
        bookshelfService.add(bookshelf);
        return ResultFactory.buildSuccessResult(null);
    }
}
